package com.weme.live.activity.liveview

import android.content.Context
import android.os.CountDownTimer
import android.util.AttributeSet
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import android.widget.TextView
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.findViewTreeViewModelStoreOwner
import com.weme.common.util.*
import com.weme.live.activity.LiveRoomActivity
import com.weme.live.databinding.ViewLiveRoomTopBinding
import com.weme.live.view.CountdownView
import com.weme.live.viewmode.LiveViewMode
import dev.kratos.api.user_server.v1.FollowRequest

class LiveRoomTopView @JvmOverloads constructor(context: Context, attributeSet: AttributeSet? = null, defStyle: Int = 0) : FrameLayout(context, attributeSet, defStyle) {


    private val viewBinding = ViewLiveRoomTopBinding.inflate(LayoutInflater.from(context), this, true)

    private var mLiveRoomVModel: LiveViewMode? = null
    var isFollow = false
    var countDownTimer: MyCountDownTimer? = null

    init {
        val activity = (context as LiveRoomActivity)
        val viewModel = ViewModelProvider(activity).get(LiveViewMode::class.java)
        viewModel.followLiveData.observe(activity) {
            if (it) {
                isFollow = true
                viewBinding.btnAttention.gone()
            }
        }
        viewBinding.imClone.setOnFastClickListener {
            viewModel.navigateToReviewPageLiveData.postValue("")//点击挂断按钮
        }
        viewModel.startTimingLiveData.observe(activity) {
            if (viewBinding.countdownView.visibility == View.VISIBLE) {
                viewBinding.countdownView.setCountdownTime(it.toLong())
            }
        }

        viewModel.updateTimingLiveData.observe(activity) {
            if (!viewModel.isMatch && viewBinding.countdownView.visibility == View.VISIBLE) {
                viewBinding.countdownView.updateTime(it.toLong())
            }
        }


        //显示快没钱的倒计时
        viewModel.finalCountdownLiveData.observe(activity) {
            if (it == -1L) { //如果等于-1 就代表 充值过有钱了
                if (viewBinding.countdownView.visibility == View.VISIBLE) {
                    return@observe //已经显示正常倒计时的情况 直接返回
                }
                countDownTimer?.cancel()
                countDownTimer?.onFinish()
                viewBinding.sclFinalCountdown.gone()
                viewBinding.vFinalCountdown.gone()
                viewBinding.countdownView.visible()
                return@observe
            }
            viewBinding.countdownView.gone()
            if (viewBinding.sclFinalCountdown.visibility == View.VISIBLE) {
                return@observe
            } else {
                viewBinding.sclFinalCountdown.visible()
                viewBinding.vFinalCountdown.visible()
                countDownTimer = MyCountDownTimer(it * 1000, 1000, viewBinding.tvTimes)
                countDownTimer?.start()
            }
        }

        viewModel.updateUserInfoLiveData.observe(activity) { it1 ->
            viewBinding.imHead.loadImage(it1.headPic)

            viewBinding.tvName.text = setNameAge(String.format(it1.nickname + "," + it1.age))
            if (it1.isFollow == 1) {
                isFollow = true
                viewBinding.btnAttention.gone()
            }
            val country = getCountryBean(it1.countryCode)
            country?.let {
                viewBinding.imCountry.loadImage(it.countryFlag.toString())
                viewBinding.tvCountry.text = it.countryName
            }

        }


    }


    //清屏
    fun setScreenClear(isClear: Boolean) {
        viewBinding.calInfo.setInvisible(!isClear)
        viewBinding.imClone.setInvisible(!isClear)
        if (isClear) {
            viewBinding.btnAttention.gone()
            viewBinding.countdownView.gone()
        } else {
            if (!isFollow) { // 没关注才是显示
                viewBinding.btnAttention.visible()
            }
            viewBinding.countdownView.visible()
        }
    }

    fun setViewMode(requireActivity: FragmentActivity) {
        mLiveRoomVModel = findViewTreeViewModelStoreOwner()?.let {
            ViewModelProvider(requireActivity)[LiveViewMode::class.java]
        }
        viewBinding.btnAttention.setOnFastClickListener {
            mLiveRoomVModel?.apply {
                setFollowApi(FollowRequest.newBuilder().setUid(anchorId).build())
            }

        }
        viewBinding.tvAddTime.setOnFastClickListener {
            requireActivity.showToast("跳转到金币商城")

        }
    }

    class MyCountDownTimer(totalMillis: Long, intervalMillis: Long,var tvTimes: TextView) : CountDownTimer(totalMillis, intervalMillis) {
        override fun onTick(millisUntilFinished: Long) {
            val secondsRemaining = millisUntilFinished / 1000
            tvTimes.text = String.format(" $secondsRemaining")
        }

        override fun onFinish() {
            tvTimes.gone()
        }
    }


    fun onDestroy() { //  viewBinding.imClone.onStopTime()
        viewBinding.countdownView.onCloneView()
        countDownTimer?.cancel()
        countDownTimer?.onFinish()
    }
}